<?php

class FinanceController extends Controller {
    
    public function actionIndex() {
        $this->render("index");
    }
    
    function actionPaymentType() {
        $model = new PaymentType();
        $this->render("PaymentType", array(
            "model" => $model
        ));
    }
    
    function actionPaymentTypeForm($id=NULL) {
        $model = new PaymentType();
        
        // Save
        if (!empty($_POST["PaymentType"])) {
            if (!empty($_POST["PaymentType"]["id"])) {
                $id = $_POST["PaymentType"]["id"];
                $model = PaymentType::model()->findByPk($id);
            }
            $model->_attributes = $_POST["PaymentType"];
            
            if ($model->save()) {
                $this->redirect(array("Finance/PaymentType"));
            }
        }
        
        // Render
        if (!empty($id)) {
            $model = PaymentType::model()->findByPk($id);
        }
        
        $this->render("PaymentTypeForm", array(
            "model" => $model
        ));
    }
    
    function actionPaymentTypeDelete($id) {
        PaymentType::model()->deleteByPk($id);
        $this->redirect("index.php?r=Finance/PaymentType");
    }
    //End Config Table mt_payment_type
    
    function actionOrderStatus() {
        $model = new Order();
        $this->render("OrderStatus", array(
            "model" => $model
        ));
    }
    
    function  actionOrderStatusForm($id=1)
    {
       $order = new Order();
       $payment = new Payment();
       
       if (!empty($_POST["Payment"]) && !empty($_POST["Order"])) {
            if (!empty($_POST["Payment"]["order_id"])) {
                $id = $_POST["Payment"]["order_id"];
                $payment = Payment::model()->findByPk($id);
                $order = Order::model()->findByPk($id);
            }
            $payment->_attributes = $_POST["Payment"];
            $order->_attributes = $_POST["Order"];
            
            if ($payment->save() && $order->save()) {
                    $this->redirect(array("Finance/OrderStatus"));
            }
        }
        
        // Render
        if (!empty($id)) {
            $order = Order::model()->findByPk($id);
            $payment = Payment::model()->findByPk($id);
        }
        
        $this->render("OrderStatusForm", array(
            "order" => $order,"payment" => $payment
        ));
    }

}

?>
